<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">用户名</label>
        <el-input v-model="query.username" clearable placeholder="用户名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">手机号码</label>
        <el-input v-model="query.phone" clearable placeholder="手机号码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否为admin账号</label>
        <el-input v-model="query.isAdmin" clearable placeholder="是否为admin账号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态：1启用、0禁用</label>
        <el-input v-model="query.enabled" clearable placeholder="状态：1启用、0禁用" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">工号</label>
        <el-input v-model="query.nu" clearable placeholder="工号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.username" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio v-model="form.gender" v-for="item in dict.gender" :key="item.id" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input v-model="form.phone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="头像地址">
            <el-input v-model="form.avatarName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="头像真实路径">
            <el-input v-model="form.avatarPath" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否为admin账号">
                未设置字典，请手动设置 Radio
          </el-form-item>
          <el-form-item label="状态：1启用、0禁用">
                未设置字典，请手动设置 Radio
          </el-form-item>
          <el-form-item label="工号">
            <el-input v-model="form.nu" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="nickName" label="昵称" />
        <el-table-column prop="gender" label="性别">
          <template slot-scope="scope">
            {{ dict.label.gender[scope.row.gender] }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号码" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="avatarName" label="头像地址" />
        <el-table-column prop="avatarPath" label="头像真实路径" />
        <el-table-column prop="password" label="密码" />
        <el-table-column prop="isAdmin" label="是否为admin账号" />
        <el-table-column prop="enabled" label="状态：1启用、0禁用" />
        <el-table-column prop="nu" label="工号" />
        <el-table-column v-if="checkPer(['admin','myUser:edit','myUser:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudMyUser from '@/api/myUser'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { userId: null, username: null, nickName: null, gender: null, phone: null, email: null, avatarName: null, avatarPath: null, password: null, isAdmin: null, enabled: null, createBy: null, updateBy: null, pwdResetTime: null, createTime: null, updateTime: null, nu: null }
export default {
  name: 'MyUser',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['gender'],
  cruds() {
    return CRUD({ title: 'myuser', url: 'api/myUser', idField: 'userId', sort: 'userId,desc', crudMethod: { ...crudMyUser }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'myUser:add'],
        edit: ['admin', 'myUser:edit'],
        del: ['admin', 'myUser:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'username', display_name: '用户名' },
        { key: 'phone', display_name: '手机号码' },
        { key: 'isAdmin', display_name: '是否为admin账号' },
        { key: 'enabled', display_name: '状态：1启用、0禁用' },
        { key: 'nu', display_name: '工号' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
